package test.reflect;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class ActionProxy {

	private Action target;
	public ActionProxy(Action target){
		this.target=target;
	}
	public String execute(Map<String,String> param) throws IllegalArgumentException, IntrospectionException, IllegalAccessException, InvocationTargetException{
		paramInteceptor(target, param);
		return target.execute();
	}
	
	/**
	 * 模拟请求参数拦截器
	 * @param action
	 * @param param
	 * @throws IntrospectionException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	private void paramInteceptor(Action action,Map<String,String> param) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		PropertyDescriptor[] ps=Introspector.getBeanInfo(action.getClass()).getPropertyDescriptors();
		for(PropertyDescriptor propertyDescriptor:ps){
			Method setter=propertyDescriptor.getWriteMethod();//获取属性的setter方法
			if(setter!=null){
				setter.invoke(action, param.get(propertyDescriptor.getName()));
			}
		}
	}
}
